﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using Microsoft.Live;
using System.Data.Linq;
using System.Xml.Linq;

namespace Common.Services
{
    public class BackUpData
    {
        
        AutoResetEvent sync = new AutoResetEvent(false);
  
        public void Uploaddata(string filenm, byte[] filestream, string skyDriveFolderID,LiveConnectClient client)
        {
            try
            {
               
                //string myString = System.Text.Encoding.UTF8.GetString(filestream, 0, filestream.Length);
                string SkyDriveFileName = "";
                string filename = "";
                string file1 = System.IO.Path.GetFileName(filenm);
                SkyDriveFileName = System.IO.Path.GetFileNameWithoutExtension(file1);
                Stream stream = new MemoryStream(filestream);
                filename = SkyDriveFileName + ".txt";
                client.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(client_UploadCompleted);
                client.UploadAsync(skyDriveFolderID, filename, true, stream, null);
                sync.WaitOne();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Some data not backup, please try again later");
            }
        }

        void client_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
           
            try
            {
                sync.Set();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Some data not backup, please try again later");
                Exception exnew = new Exception("Exception in backup Method In Settings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                throw exnew;
            }
        }

        //public void GetFolderId(IDictionary<string, object> iDictionary, LiveConnectClient lcc, string skyDriveFolderName)
        //{
        //    client = lcc;
        //    Dictionary<string, object> folderData = (Dictionary<string, object>)iDictionary;
        //    List<object> folders = (List<object>)folderData["data"];

        //    foreach (object item in folders)
        //    {
        //        Dictionary<string, object> folder = (Dictionary<string, object>)item;
        //        if (folder["name"].ToString() == skyDriveFolderName)
        //            skyDriveFolderID = folder["id"].ToString();


        //    }

        //    if (skyDriveFolderID == string.Empty)
        //    {
        //        Dictionary<string, object> skyDriveFolderData = new Dictionary<string, object>();
        //        skyDriveFolderData.Add("name", skyDriveFolderName);
        //        //You can add a folder description, but for some reason it does not work.
        //        //folderData.Add("description", "Folder for storing files from my WP7 app isolated storage.");
        //        client.PostCompleted += new EventHandler<LiveOperationCompletedEventArgs>(CreateFolder_Completed);
        //        client.PostAsync("me/skydrive", skyDriveFolderData);
        //    }
        //}

        //void CreateFolder_Completed(object sender, LiveOperationCompletedEventArgs e)
        //{
        //    client.PostCompleted -= this.CreateFolder_Completed;
        //    try
        //    {
        //        if (e.Error == null)
        //        {
        //            Dictionary<string, object> folder = (Dictionary<string, object>)e.Result;
        //            skyDriveFolderID = folder["id"].ToString();
        //        }
        //        else
        //        {
        //            MessageBox.Show(e.Error.Message);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Some data not backup, please try again later");
        //        Exception exnew = new Exception("Exception in backup Method In Settings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
        //        throw exnew;
        //    }
        //}


    }
}
